


package com.example.actearly_revised;

import com.example.actearly_revised.QuestionsObject.Node;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.view.ViewGroup;

import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;


import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class QuicklistPage extends Activity {

	String question[];
	String keys[];
	SQLogic conn;

	Node question_node;
	String milestoneNames[];
	TextView currentMilestone;
	ListView listview1;
	String ageGroups[];
	Spinner quicklistAgeSpinner;
	int[] ageID;
	int index;
	QuestionsObject db;
	ArrayAdapter<String> quicklistAdapter;
	ArrayList<ExpandListGroup> expandGroup;
	int position;
	static int milestone;
	QuicklistTabHomePage parent;

	//** Called when the activity is first created. *//*
	private ExpandListAdapter ExpAdapter;
	private ArrayList<ExpandListGroup> ExpListItems;
	private ExpandableListView ExpandList;
	@Override
	public void onCreate(Bundle savedInstanceState) {

		ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};


		super.onCreate(savedInstanceState);
		// set background
		setContentView(R.layout.activity_quick_list_page);
		RelativeLayout pageLayout=(RelativeLayout)findViewById(R.id.quicklistLayout);
		//pageLayout.setBackgroundResource(R.drawable.background);
		ExpandList = (ExpandableListView) findViewById(R.id.ExpList);

		// grabs milestone from parent
		parent = (QuicklistTabHomePage) this.getParent(); 
		milestone = parent.currentmilestone;


		//shift dropdown arrow to the RIGHT instead of default RIGHT position
		Display tempDisplay= getWindowManager().getDefaultDisplay();
		int width = tempDisplay.getWidth();
		ExpandList.setIndicatorBounds(width-50, width);


		ExpListItems = SetStandardGroups(5);
		ExpAdapter = new ExpandListAdapter(QuicklistPage.this, ExpListItems);
		ExpandList.setAdapter(ExpAdapter);

		/*quicklistAgeSpinner = (Spinner) findViewById(R.id.listSpinner);
		ArrayAdapter<CharSequence> quicklistAdapter = ArrayAdapter.createFromResource(
				this, R.array.milestoneAges, android.R.layout.simple_spinner_item);

		quicklistAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		quicklistAgeSpinner.setAdapter(quicklistAdapter);
		quicklistAgeSpinner.setSelection(5);
		 */
		String[] items= getResources().getStringArray(R.array.milestoneAges);
		quicklistAgeSpinner = (Spinner) findViewById(R.id.listSpinner);
		ArrayAdapter<String> quicklistAdapter = new ArrayAdapter<String>(this,
				R.layout.activity_spinner_layout, items) {

			public View getView(int position, View convertView, ViewGroup parent) {
				View v = super.getView(position, convertView, parent);

				Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				((TextView) v).setTypeface(externalFont);
				((TextView) v).setTextColor(-16777216);


				return v;
			}

			public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
				View v =super.getDropDownView(position, convertView, parent);

				Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				((TextView) v).setTypeface(externalFont);

				return v;
			}
		};

		quicklistAdapter.setDropDownViewResource(R.layout.activity_spinner_layout);
		quicklistAgeSpinner.setAdapter(quicklistAdapter);
		quicklistAgeSpinner.setSelection(5);



		parent.currentmilestone = ageID[5];




		quicklistAgeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				ExpListItems = SetStandardGroups(position);
				//((TextView) arg1).setTextColor(-1);

				milestone = ageID[position];
				parent.currentmilestone = milestone;

				ExpAdapter = new ExpandListAdapter(QuicklistPage.this, ExpListItems);
				ExpandList.setAdapter(ExpAdapter);
			}

			public void onNothingSelected(AdapterView<?> arg0) {

				// do nothing
			}

		});     

	}

	@Override
	protected void onResume(){
		super.onResume();
		milestone = parent.currentmilestone;

		// find index of current milestone
		int CurrentAgeIndex = -1;
		for(int i = 0; i < ageID.length; i++){
			if(ageID[i] == milestone){
				CurrentAgeIndex = i;
				break;
			}
			CurrentAgeIndex = -1;
		}

		quicklistAgeSpinner.setSelection(CurrentAgeIndex);

	}

	public ArrayList<ExpandListGroup> SetStandardGroups(int spinnerIndex) {
		String category[]= getResources().getStringArray(R.array.milestoneCategories);
		expandGroup= new ArrayList<ExpandListGroup>();
		ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
		ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
		//index= quicklistAgeSpinner.getSelectedItemPosition();
		for (int i=0;i<category.length;i++)
		{
			expandGroup.add( new ExpandListGroup());
			expandGroup.get(i).setName(category[i]);
			conn = new SQLogic(QuicklistPage.this);
			conn.open();

			if (i==0)
			{
				db = conn.RetrieveQuestions(ageID[spinnerIndex],0,"s");
			}
			else if (i==1)
			{
				db = conn.RetrieveQuestions(ageID[spinnerIndex],0,"c");
			}
			else if (i==2)
			{
				db = conn.RetrieveQuestions(ageID[spinnerIndex],0,"l");
			}
			else if (i==3)
			{
				db = conn.RetrieveQuestions(ageID[spinnerIndex],0,"m");
			}
			conn.close();
			// set new questions object up with key values
			question= new String[db.count()];
			System.out.println(question);
			String keys[] = new String[db.count()];
			db.getMap().keySet().toArray(keys);
			
			//	keys aren't actually sorted in order, so need to do that here		
			int keyInt[]= new int[db.count()];

			for (int j=0; j<db.count();j++){
				keyInt[j]=Integer.parseInt(keys[j]);
			}
			Arrays.sort(keyInt);
			for (int j=0; j<db.count();j++){
				keys[j]=Integer.toString(keyInt[j]);
			}
			
			// ok keys are in order now
			for (int j=0; j<db.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
			{
				Node question_node = db.getQuestions(keys[j]);
				question[j]= question_node.Question;
			}

			for (int j=0; j<question.length; j++)
			{
				ExpandListChild temp= new ExpandListChild();
				temp.setName(question[j]);
				temp.setTag(null);
				list2.add(temp);
			}
			expandGroup.get(i).setItems(list2);
			list2 = new ArrayList<ExpandListChild>();
			list.add(expandGroup.get(i));
		}
		return list;
	}


}



/*

//THIS CODE SHOWS HOW TO USE VIEW PAGER TO SWIPE BETWEEN PAGES
//WORKS GREAT BUT THE ONLY ISSUE IS YOU NEED TO MANUALLY CREATE EACH
//PAGE, SO NO DYNAMICALLY CREATING/DELETING PAGES :(


			//set up however many pages you want to have here
			//SECTION STARTS HERE
			ListView listview1 = new ListView(mContext);
			ListView listview2 = new ListView(mContext);
			ListView listview3 = new ListView(mContext);
			ListView listview4 = new ListView(mContext);
			ListView listview5 = new ListView(mContext);
			ListView listview6 = new ListView(mContext);
			ListView listview7 = new ListView(mContext);

			Vector<View> pages = new Vector<View>();
			pages.add(listview1);
			pages.add(listview2);
			pages.add(listview3);
			pages.add(listview4);
			pages.add(listview5);
			pages.add(listview6);
			pages.add(listview7);

			ViewPager vp = (ViewPager) findViewById(R.id.qlViewPager);
			QuicklistPageSwiper adapter = new QuicklistPageSwiper(mContext,pages);
			vp.setAdapter(adapter);

			TextView milestoneHeader= (TextView) findViewById(R.id.textView1);
			int currentPage= vp.getCurrentItem();
			String milestoneNames[]={"2","4","6","9","12","18","24"};
			milestoneHeader.setText("What babies do at " +milestoneNames[currentPage]+" months old");

			listview1.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question2));
			listview2.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question4));
			listview3.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question6));
			listview4.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question9));
			listview5.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question12));
			listview6.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question18));
			listview7.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question24));

 */